<?php

namespace app\common\model\xiluhouse\market;

use app\common\model\xiluhouse\Base;
use app\common\model\xiluhouse\User;
use app\common\model\xiluhouse\Village;
use think\Model;
use traits\model\SoftDelete;

class MarketHouse extends Base
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiluhouse_market_house';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getIsElevatorList()
    {
        return ['0' => __('Is_elevator 0'), '1' => __('Is_elevator 1')];
    }

    public function getIsUniqueList()
    {
        return ['0' => __('Is_unique 0'), '1' => __('Is_unique 1')];
    }

    public function getIsPublicList()
    {
        return ['0' => __('Is_public 0'), '1' => __('Is_public 1')];
    }

    public function getPropertyRightList()
    {
        return ['1' => __('Property_right 1'), '2' => __('Property_right 2')];
    }

    public function getViewStatusList()
    {
        return ['1' => __('View_status 1'), '2' => __('View_status 2'), '3' => __('View_status 3'), '4' => __('View_status 4')];
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }

    // 处理多图的情况
    public function getImagesArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $images_arr = $value ? explode(',', $value) : [];
        foreach ($images_arr as $now_image_key => $now_image_value) {
            $images_arr[$now_image_key] = cdnurl($now_image_value,true);
        }
        return $images_arr;
    }

    // 处理多图的情况
    public function getRoomImagesTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['room_images']) ? $data['room_images'] : '');
        $images_arr = $value ? explode(',', $value) : [];
        foreach ($images_arr as $now_image_key => $now_image_value) {
            $images_arr[$now_image_key] = cdnurl($now_image_value,true);
        }
        return $images_arr;
    }

    public function getVideoFileTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['video_file']) ? $data['video_file'] : '');
        return cdnurl($value,true);
    }

    public function village(){
        return $this->belongsTo(Village::class,'village_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function village2(){
        return $this->belongsTo(Village::class,'village_id','id',[],'INNER')->setEagerlyType(0);
    }

    public function user(){
        return $this->belongsTo(User::class,'user_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function purpose(){
        return $this->belongsTo(MarketPurpose::class,'purpose_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function decoration(){
        return $this->belongsTo(MarketDecoration::class,'decoration_id','id',[],'LEFT')->setEagerlyType(0);
    }

}
